package org.basis.groovy.helper;

import lombok.extern.slf4j.Slf4j;
import org.basis.enhance.groovy.helper.RegisterScriptHelper;
import org.basis.enhance.groovy.loader.ScriptLoader;
import org.basis.enhance.groovy.registry.ScriptRegistry;
import org.basis.groovy.config.properties.GroovyFlowableLoaderProperties;
import org.springframework.lang.NonNull;

import java.util.Map;

/**
 * 注册脚本
 *
 * @author wenpan 2022/09/30 21:09
 */
@Slf4j
public class RegisterScriptToFlowableHelper implements RegisterScriptHelper {


    private ScriptLoader redisScriptLoader;

    private GroovyFlowableLoaderProperties groovyFlowableLoaderProperties;

    public RegisterScriptToFlowableHelper( ScriptLoader redisScriptLoader,
                                          GroovyFlowableLoaderProperties groovyFlowableLoaderProperties) {
        this.redisScriptLoader = redisScriptLoader;
        this.groovyFlowableLoaderProperties = groovyFlowableLoaderProperties;
    }

    @Override
    public boolean registerScript(@NonNull String name, @NonNull String content, boolean allowCover) throws Exception {
        return true ;
    }

    @Override
    public boolean batchRegisterScript(@NonNull Map<String, String> scriptMap, boolean allowCover) throws Exception {
        log.warn("batch register script start.");
        scriptMap.forEach((name, content) -> {
            try {
                registerScript(name, content, allowCover);
            } catch (Exception e) {
                throw new RuntimeException("register failed，please retry.", e);
            }
        });
        log.warn("batch register script success.");
        return true;
    }

}
